<template>
  <div id="home">
    <nav-bar class="home-nav"><div slot="center">
      购物街
    </div></nav-bar>
		
    <tabcontrol class="control"
    :titles="['流行','新款','精选']"
     @tabclick="tabclick"
     ref="tabControl1"
		 v-show="isFixed"/>
     
    <scroll class="content" ref="scroll"
    @scrollY="scrollY" 
    :probetype="3"
    @pullingup="loadMore"
	:pull-up-load="true">
<home-swiper 
:banners="banners"
@imgload="settabtop"/>
<home-recommed :recommends="recommends"/>
<feature/>
<tabcontrol  
:titles="['流行','新款','精选']"
 @tabclick="tabclick"
 ref="tabControl2" />
<goods-list :goods="goodstype"/>
</scroll>

<back-top @click.native="backClick" v-show="isShowBackTop"/>
  </div></template>

<script>
  import HomeSwiper from './childComps/HomeSwiper'
  import HomeRecommed from './childComps/HomeRecommed.vue'
  import feature from './childComps/feature.vue'
  
  import NavBar from 'components/common/navbar/NavBar'
  import tabcontrol from 'components/common/tabcontrol/tabcontrol.vue'
  import goodsList from 'components/content/goods/goodsList'
  import scroll from 'components/common/scroll/scroll'
  import backTop from 'components/content/backtop/backTop'
  
  import { backTopMixin } from "@/common/mixin.js"
  import { getHomeMultidata,getHomeData } from "@/network/home.js"
  export default {
    name: "Home",
    components: {
    NavBar,
HomeSwiper,
HomeRecommed,
feature,
tabcontrol,
goodsList,
scroll
      },
      mixins:[backTopMixin],
      data(){
        return{
          // result:null
          banners:[],
          recommends:[],
          goods:{
            "pop":{page:0,list:[]},
            "new":{page:0,list:[]},
            "sell":{page:0,list:[]}
          },
          currentType:'pop',
          isoff:false,
          timer: null,
					tabOffsetTop:0,
					isFixed:false,
					saveY:0
        }
      },
      computed:{
        goodstype(){
          return this.goods[this.currentType].list
        }
      },
	  mounted() {
		  // 获取tabcontrol的offsettop,
		  // 所有的组件都有一个$el的属性,用于获取组件中的元素
	  	// console.log(this.$refs.tabControl.$el.offsetTop);
	  },
			destroyed() {
				console.log('home destroyed');
			},
			activated() {
				console.log(this.saveY);
        this.$refs.scroll.bs.refresh()
				this.$refs.scroll.backscoll(0,this.saveY,0)
			},
			deactivated() {
				this.saveY = this.$refs.scroll.getScrollY()
        // console.log(this.saveY);
			},
      methods:{
        /**
         * 事件监听
         */
				settabtop(){
					 this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop
				},
        loadMore(){
        this.getHomeGoods(this.currentType)
        this.$refs.scroll.finishUpLoad()

          // this.$refs.scroll.refreshLoad()
        },
        scrollY(options){
          // console.log(options);
        // 当y值大于1000则显示回到顶部
					this.demo(options)
					// 监听是否超过tabcontrol的offsettop
					this.isFixed = (-options.y) > this.tabOffsetTop
        },
        tabclick(index){
          switch (index){
            case 0:
            this.currentType = 'pop'
              break;
              case 1:
              this.currentType = 'new'
                break;
                case 2:
                this.currentType = 'sell'
                  break;
          }
					this.$refs.tabControl2.currentIndex = index
					this.$refs.tabControl1.currentIndex = index
        },
        /*
        *网络请求相关
        */
        getHomeMultidata(){
          getHomeMultidata().then(res=>{
            // this.result = res
              // console.log(res);
            this.banners = res.data.banner.list
            this.recommends = res.data.recommend.list
          })
        },
        getHomeGoods(type){

          const page = this.goods[type].page + 1

          getHomeData(type, page).then(res=>{
          this.goods[type].list.push(...res.data.list)
          this.goods[type].page += 1

          })
          
        }
        
      },
      created() {
this.getHomeMultidata()
    this.getHomeGoods("pop")
    this.getHomeGoods("new")
    this.getHomeGoods("sell")
      }
  }
</script>

<style scoped>
  .control{
    position: relative;
    top: 0px;
		z-index: 9;
  }
  #home{
    /* padding-top: 44px; */
    height: 100vh;
  }
.home-nav{
  background-color: var(--color-tint);
  color:white
}
.content{
  height: calc(100% - 93px);
  /* margin-top: 44px !important; */
	overflow: hidden;
	position: absolute;
	top: 44px;
	left: 0;
	right: 0;
}
</style>
